package com.ljl.forum_1.services.impl;

import com.ljl.forum_1.common.AppResult;
import com.ljl.forum_1.common.ResultCode;
import com.ljl.forum_1.dao.BoardMapper;
import com.ljl.forum_1.exception.ApplicationException;
import com.ljl.forum_1.model.Board;
import com.ljl.forum_1.services.IBoradService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Keafmd
 *
 * @ClassName: BoradServiceImpl
 * @Description: BoradServiceImpl
 * @author: lijl
 * @date: 2025/2/8 12:49
 * @Blog: https://keafmd.blog.csdn.net/
 */
@Slf4j
@Service
public class BoradServiceImpl implements IBoradService {

    @Resource
    private BoardMapper boardMapper;

    @Override
    public List<Board> selectByNum(Integer num) {
        // 非空校验
        if (num <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        // 调用DAO查询数据库中的数据
        List<Board> result = boardMapper.selectByNum(num);
        // 返回结果
        return result;
    }

    @Override
    public Board selectById(Long id) {
        // 非空校验
        if (id == null || id <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        // 调用DAO查询数据
        Board board = boardMapper.selectByPrimaryKey(id);
        // 返回结果
        return board;
    }

    @Override
    public void addOneArticleCountById(Long id) {
        // 非空校验
        if (id == null || id <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        // 查询对应板块
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null) {
            // 打印日志
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString() + ", board id = " + id);
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        // 更新板块下的帖子数量
        Board updataBoard = new Board();
        updataBoard.setId(board.getId());
        updataBoard.setArticleCount(board.getArticleCount() + 1);
        // 调用DAO执行SQL
        int row = boardMapper.updateByPrimaryKeySelective(updataBoard);
        if (row != 1) {
            // 打印日志
            log.warn(ResultCode.FAILED.toString() + ", 受影响的行数不等于一");
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }

    @Override
    public void subOneArticleCountById(Long id) {
        // 非空校验
        if (id == null || id <= 0) {
            // 打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
        // 查询对应板块
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board == null) {
            // 打印日志
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString() + ", board id = " + id);
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        // 更新板块下的帖子数量
        Board updataBoard = new Board();
        updataBoard.setId(board.getId());
        updataBoard.setArticleCount(board.getArticleCount() - 1);
        // 判断减1之后是否小于0
        if (updataBoard.getArticleCount() < 0) {
            updataBoard.setArticleCount(0);
        }
        // 调用DAO执行SQL
        int row = boardMapper.updateByPrimaryKeySelective(updataBoard);
        if (row != 1) {
            // 打印日志
            log.warn(ResultCode.FAILED.toString() + ", 受影响的行数不等于一");
            // 抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }
}
